﻿using System.Windows.Interop;

namespace IFoxCAD.WPF;

public static class WpfEx
{
    /// <summary>
    /// 是非模态窗口
    /// </summary>
    /// <param name="window">窗日</param>
    /// <returns>是则返回<c>true</c></returns>
    public static bool IsModelessWindow(this Window window)
    {
        var windowInteropHelper = new WindowInteropHelper(window);
        return windowInteropHelper.Owner == IntPtr.Zero;
    }

    /// <summary>
    /// 设置窗体的DialogResult<br/>
    /// 模态窗体设置DialogResult会触发窗体的Close函数<br/>
    /// 非模态窗体不会设置并返回<c>false</c>
    /// </summary>
    /// <param name="window">窗口</param>
    /// <param name="value">DialogResult值</param>
    /// <returns>设置成功返回<c>true</c></returns>
    public static bool SetDialogResult(this Window window,bool value)
    {
        if (!window.IsModelessWindow())
            return false;
        window.DialogResult = value;
        return true;
    }
}